package com.symaster.common.animation.view.frame;

import com.symaster.common.animation.CurvePoint;
import com.symaster.common.geom.entity.LineSegment;
import com.symaster.common.geom.entity.Point;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author yinmiao
 * @version 2023/11/2
 */
public class CurveCreateCanvas extends JComponent {

    private static final int longTermLength = 15;
    private static final int shortTermLength = 8;

    private List<LineSegment> lineSegments;
    private List<CurvePoint> curvePoints; // 控制点列表
    private Integer selectCurvePoint;

    /**
     * 更新控制点
     *
     * @param lineSegments 线段列表
     */
    public void updateLineSegments(List<LineSegment> lineSegments) {
        this.lineSegments = lineSegments;
        repaint();
    }

    public void updateCurvePoints(List<CurvePoint> curvePoints) {
        this.curvePoints = curvePoints;
        repaint();
    }

    public void setSelectCurvePoint(Integer selectCurvePoint) {
        if (this.selectCurvePoint == null && selectCurvePoint == null) {
            return;
        }
        if (this.selectCurvePoint != null && this.selectCurvePoint.equals(selectCurvePoint)) {
            return;
        }
        this.selectCurvePoint = selectCurvePoint;
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        SwingUtil.antialiasingOn((Graphics2D) g);

        g.setColor(new Color(222, 230, 237));
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(new Color(169, 169, 169));
        g.drawString("100", 0, 12);
        g.drawString("50", longTermLength + 2, getHeight() / 2 + 5);
        g.drawString("0", 0, getHeight() - 2);
        g.drawString("50", getWidth() / 2 - 6, getHeight() - 2 - longTermLength);
        g.drawString("100", getWidth() - 22, getHeight() - 2);

        // 垂直分段
        g.fillRect(0, getHeight() / 2 / 2, shortTermLength, 1);
        g.fillRect(0, getHeight() / 2, longTermLength, 1);
        g.fillRect(0, getHeight() / 2 / 2 + getHeight() / 2, shortTermLength, 1);

        // 水平分段
        g.fillRect(getWidth() / 2 / 2, getHeight() - shortTermLength, 1, shortTermLength);
        g.fillRect(getWidth() / 2, getHeight() - longTermLength, 1, longTermLength);
        g.fillRect(getWidth() / 2 / 2 + getWidth() / 2, getHeight() - shortTermLength, 1, shortTermLength);

        g.setColor(new Color(169, 169, 169));
        g.drawLine(100, getHeight() / 2, getWidth() - 100, getHeight() / 2);
        g.drawLine(getWidth() / 2, 80, getWidth() / 2, getHeight() - 80);

        if (lineSegments != null) {
            g.setColor(new Color(65, 218, 23));
            for (LineSegment lineSegment : lineSegments) {
                Point startPoint = lineSegment.getStartPoint();
                Point endPoint = lineSegment.getEndPoint();

                int x1 = (int) (startPoint.getX() * getWidth());
                int y1 = (int) (startPoint.getY() * getHeight());
                int x2 = (int) (endPoint.getX() * getWidth());
                int y2 = (int) (endPoint.getY() * getHeight());

                g.drawLine(x1, y1, x2, y2);
            }
        }

        if (curvePoints != null) {
            for (int i = 0; i < curvePoints.size(); i++) {
                CurvePoint curvePoint = curvePoints.get(i);

                if (selectCurvePoint != null && i == selectCurvePoint) {
                    g.setColor(new Color(0, 89, 255));
                } else {
                    g.setColor(new Color(0, 72, 206));
                }

                int x = (int) (curvePoint.getX() * getWidth());
                int y = (int) (curvePoint.getY() * getHeight());
                g.fillOval(x - 3, y - 3, 7, 7);
            }
        }

    }
}
